package com.cbgolf.oa.model;

import android.app.Activity;
import android.support.v4.util.ArrayMap;
import com.cbgolf.oa.base.BaseModel;
import com.cbgolf.oa.entity.CodeTypeBean;
import com.cbgolf.oa.entity.NetResponse;
import com.cbgolf.oa.entity.TeeTimeBean;
import com.cbgolf.oa.entity.TeeTimeBean.TeeTimesBean;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.NetUtils;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.presenter.ITEETIMEPresenter;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.ErrorUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Created by lq on 2018/1/5.
 */

public class TeetimeModel extends BaseModel implements ITEETIMEPresenter.Model {

  private ITEETIMEPresenter worker;
  private String start;
  private String codeType;

  public TeetimeModel(Activity act, ITEETIMEPresenter worker) {
    this.worker = worker;
    super.init(act);
  }

  @Override
  protected void getInitData() {
    start = DateUtil.getDayBegin().getTime() + "";
    getCodeType();
  }


  private void getCodeType() {
    Web.getOK(WebAPI.park_coures_select_get, new NetCallBack(response -> {
      List<CodeTypeBean> listCode = getList(response, CodeTypeBean.class);
      if (Util.listIsNull(listCode)) {
        worker.getCodeTypeFail(errorMsg, errorCode);
      } else {
        sortCodeList(listCode);
      }
    }));
  }

  private void sortCodeList(List<CodeTypeBean> listCode) {
    List<String> list = new ArrayList<>();
    for (int i = 0; i < listCode.size(); i++) {
      list.add(listCode.get(i).codeType);
    }
    Collections.sort(list);
    codeType = list.get(0);
    worker.getCodeTypeSuccess(list);
    getTeeTime(start, codeType);
  }

  public void refresh() {
    getTeeTime(start, codeType);
  }

  public void getTeeTime(String sTime, String codeType) {
    if (!Util.isNull(sTime)) {
      this.start = sTime;
    }
    this.codeType = Util.isNull(codeType) ? this.codeType : codeType;
    if (!Util.isNetWorkConnected()) {
      worker.getDataFail(ErrorUtil.NONET, ErrorUtil.NONET_CODE);
      return;
    }
    Map<String, String> map = new ArrayMap<>();
    map.put("teeTimeDate", TextUtil.Text(this.start));
    map.put("codeType", TextUtil.Text(codeType));
    worker.showProgress();
    Web.getOK(WebAPI.teetime_get,
        map, new NetCallBack(response ->
            beanResult(response, TeeTimeBean.class, worker)
        ));
  }


  @Override
  public void addPerson(TeeTimesBean bean) {
    Web.postOk(WebAPI.teetime_book_post + bean.teeTimeId, new NetCallBack(this::reduceAddResult));
  }

  @Override
  public void reducePerson(TeeTimesBean bean) {
    Web.deleteOk(WebAPI.teetime_book_delete + bean.teeTimeId,
        new NetCallBack(this::reduceAddResult));
  }

  private void reduceAddResult(NetResponse response) {
    if (NetUtils.SUCCESS == response.statusCode) {
      getTeeTime(start, codeType);
    } else {
      worker.getDataFail(Util.isNull(response.errorMsg) ? ErrorUtil.NETERROR : response.errorMsg);
    }
  }
}
